package com.bang.springcloud.report.consumer;

import com.bang.framework.common.FastJsonConvertUtil;
import com.bang.framework.mq.constants.QueueConstants;
import com.bang.springcloud.report.entity.Order;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName: OrderConsumer
 * @author: BANG
 * @Date: 2019/12/10 14:40
 * @Version: 1.0
 * @Modified By:
 * @Desc: TODO
 */
@Component
@Slf4j
public class OrderConsumer {


    /**
     * @Desc: 处理order消息
     * @Author: <BANG>
     * @Date: 2019/12/10 0010 下午 2:43
     * @Param: order
     * @Param: channel
     * @Param: headers
     */
    @RabbitListener(queues = QueueConstants.Queue.BANG_TEST_DEFAULT_QUEUE)
    @RabbitHandler
    public void onOrderMessage(@Payload Order order,
                               Channel channel,
                               @Headers Map<String, Object> headers) throws Exception {
        log.error("--------------------------------------");
        log.error("消费端order: {}", FastJsonConvertUtil.convertObjectToJSON(order));
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            log.debug("处理业务逻辑>>>>>>>>>>>>>>>>>>>>>>>> deliveryTag:{}", deliveryTag);
            log.debug("通知客服,用户已下单>>>>>>>>>>>>>>>>>");
            //手工ACK
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            log.error("处理order出现异常,记录异常日志>>>>>>>>>>>>");
            log.error("正在记录日志>>>>>>>>>>>>{}", e.getMessage());
        }
    }
}
